﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using eCommerce.db.DataRespository;
using eCommerce.db.SQLDataAccess;

namespace eCommerce.db.DALRespository
{
    public class SizeDal
    {
        #region SizeDal private
        private static void SetInfo(IDataRecord reader, ref SizeEntity o)
        {
            for (int i = 0; i < reader.FieldCount; i++)
            {
                PropertyInfo propertyInfo = DbCommon.GetPropertyInfo(o, reader.GetName(i));
                if (((propertyInfo != null) && (reader[i] != null)) && (reader[i] != DBNull.Value))
                    propertyInfo.SetValue(o, reader[i], null);
                else
                    o[reader.GetName(i)] = reader[i];
            }
        }

        private static void SetInfoList(ref SqlDataReader reader, ref List<SizeEntity> imageList)
        {
            while (reader.Read())
            {
                var o = new SizeEntity();
                SetInfo(reader, ref o);
                imageList.Add(o);
            }
            reader.Dispose();
        }
        #endregion

        /// <summary>
        /// Danh sách kích thước cửa hàng và sản phẩm đó đã được phân phối
        /// </summary>
        /// <param name="storeId"></param>
        /// <param name="productId"></param>
        /// <returns></returns>
        public static List<SizeEntity> ListSizeExistInStoreByStoreProduct(int storeId, int productId)
        {
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            var lstSize = new List<SizeEntity>();
            try
            {
                var reader = SqlHelper.ExecuteReader(DbCommon.ConnectionString,
                                DbCommon.DatabaseSchema + "eCommerce_List_Size_Exist_InStore_By_Store_Product", storeId, productId);
                SetInfoList(ref reader, ref lstSize);
                return lstSize;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "eCommerce_List_Size_Exist_InStore_By_Store_Product: {0}", ex.Message));
            }
        }    

        public static List<SizeEntity> GetListByType(int typeId)
        {
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            var lstSize = new List<SizeEntity>();
            try
            {
                var reader = SqlHelper.ExecuteReader(DbCommon.ConnectionString, DbCommon.DatabaseSchema + "Size_GetListByType", typeId);
                SetInfoList(ref reader, ref lstSize);
                return lstSize;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "Size_GetListByType: {0}", ex.Message));
            }
        }

        public static SizeEntity GetById(int id)
        {
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            var image = new SizeEntity();
            try
            {
                var reader = SqlHelper.ExecuteReader(DbCommon.ConnectionString, DbCommon.DatabaseSchema + "Size_GetById", id);
                if (reader.Read())
                    SetInfo(reader, ref image);
                return image;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "Size_GetById: {0}", ex.Message));
            }
        }
     
        public static void Save(SizeEntity o)
        {
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            try
            {
                if (o.SizeId > 0)
                {
                    SqlHelper.ExecuteNonQuery(
                        DbCommon.ConnectionString,
                        DbCommon.DatabaseSchema + "Size_Update", o.SizeId, o.Name, o.Priority);
                }
                else
                {
                    SqlHelper.ExecuteNonQuery(
                        DbCommon.ConnectionString,
                        DbCommon.DatabaseSchema + "Size_Create", o.Name, o.Priority);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "Size Save: {0}", ex.Message));
            }
        }

        public static void Delete(int id)
        {
            SqlHelper.ExecuteNonQuery(DbCommon.ConnectionString, DbCommon.DatabaseSchema + "Size_Delete", id);
        }      
    }
}
